package show.me.the.money.util.exp.standard;

import java.util.HashMap;

import org.hibernate.SessionFactory;
import org.hibernate.impl.SessionFactoryImpl;
import org.springframework.web.context.WebApplicationContext;

import show.me.the.money.util.AppContextHolder;


public abstract class ExpUtil {
	
	private static HashMap<String, String> mssqltypes = new HashMap<String,String>();
	static{
		mssqltypes.put("yyyy-mm","convert(char(7),{0},121)");
		mssqltypes.put("yyyy-mm-dd", "convert(char(10),{0},121)");
	}
	
	private static HashMap<String, String> mysqltypes = new HashMap<String,String>();
	static{
		mysqltypes.put("yyyy-mm", "date_format({0},'%Y-%m')");
		mysqltypes.put("yyyy-mm-dd", "date_format({0},'%Y-%m-%d')");
	}
	
	private static HashMap<String,String> orcltypes=new HashMap<String,String>();
	static{
		orcltypes.put("yyyy-mm", "to_char({0},'yyyy-mm')");
		orcltypes.put("yyyy-mm-dd", "to_char({0},'yyyy-mm-dd')");
	}
	
	public static String getType(String t){
		WebApplicationContext wac=AppContextHolder.get();
		SessionFactory sf = (SessionFactory) wac
		.getBean("mySessionFactory");
		String dialectName=((SessionFactoryImpl)sf).getDialect().getClass().getName();
		if(dialectName.contains("MySQL")){
			return mysqltypes.get(t);
		}else if(dialectName.contains("SQLServer")){
			return mssqltypes.get(t);
		}else{
			return orcltypes.get(t);
		}
	}
}
